package Test.offer;


import java.util.ArrayList;

/**
 * @ClassName P34
 * @Description
 * @Company inspur
 * @Author Kevin
 * @Date 2020/8/1 17:57
 * @Version 1.0
 */
public class P34 {

    public void findPath(TreeNode root, int k) {
        if (root == null) return;
        ArrayList<Integer> list = new ArrayList<>();
        dfs(root, k, list);
    }

    private void dfs(TreeNode root, int k, ArrayList<Integer> list) {
        if (root == null) return;
        list.add(root.val);
        k -= root.val;

        if (root.left == null && root.right == null && k == 0) {
            for (Integer i: list) {
                System.out.print(i + " ");
            }
            System.out.println();
        } else {
            if (root.left != null) dfs(root.left, k, list);
            if (root.right != null) dfs(root.right, k, list);
        }
        list.remove(list.size()-1);
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    }
}
